/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_orm.htm
 *
 * Zhiqim Orm is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.orm.dbo.condition;

import java.util.ArrayList;
import java.util.List;

import org.zhiqim.kernel.extend.MapSO;
import org.zhiqim.orm.ORMException;
import org.zhiqim.orm.dbo.Condition;
import org.zhiqim.orm.dbo.Dbo;

/**
 * 或者条件，里面是一个条件列表，格式如：(field = 'abc' or field2 <> 'def')
 *
 * @version v1.0.0 @author zouzhigang 2014-3-21 新建与整理
 */
public class _Or extends Condition
{
    List<Condition> list = new ArrayList<Condition>(2);
    
    public void addCondition(Condition condition)
    {
        list.add(condition);
    }
    
    public boolean match(Object obj)
    {
        for (Condition c : list)
        {
            if (c.match(obj))
                return true;
        }
        
        return false;
    }
    
    public String toSql(Dbo dbo, MapSO paramMap) throws ORMException
    {
        if (list.size() < 2)
            throw new ORMException("ZTable/ZView[or]要求的条件数目必须>=2个");
        
        StringBuilder strb = new StringBuilder("(");
        //第一个
        strb.append(list.get(0).toSql(dbo, paramMap));
        //后面的
        for (int i=1;i<list.size();i++)
        {
            strb.append(_SPACE_).append(OR).append(_SPACE_)
                .append(list.get(i).toSql(dbo, paramMap));
        }
        strb.append(")");
        
        return strb.toString();
    }
    
    public List<Condition> getCondition()
    {
        return list;
    }
}
